#include "scene.h"
#include<QSoundEffect>
#include<QMediaPlayer>
#include<QtMultimedia/QtMultimedia>
#include<QtMultimediaWidgets/QVideoWidget>
#include<QDebug>
#include<QAudioOutput>
#include<QPainter>
#include<scene.h>
#include"button.h"
#include"map.h"


//主页面创建

Scene::Scene(QWidget *parent) : QMainWindow(parent)
{

    this->setFixedSize(902,602);
    srand(uint(QTime(0,0,0).secsTo(QTime::currentTime())));//生成随机数
    //设置并播放背景音乐
    QSoundEffect * sound = new QSoundEffect(this);
    sound->setSource(QUrl::fromLocalFile(""));
    //sound->setLoopCount(5);这里背景音乐无法循环播放
    //sound->setVolume(0.25f);
    sound->setLoopCount(QSoundEffect::Infinite);//循环播放
    sound->setVolume(0.25f);//设置声音大小
    sound->play();
    //创建计时器
    timer = new QTimer;
    //场景创建
    scene = new QGraphicsScene(this);
    //设置场景过程
    scene->setSceneRect(150, 0, 900, 600);
    scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    Shop *shop = new Shop;//设置购买栏
    shop->setPos(520, 45);//调整购买栏大小
    scene->addItem(shop);
    //设置爪子
    Claw *claw = new Claw;
    claw->setPos(830, 40);
    scene->addItem(claw);
    //设置按钮
    Button *button = new Button(sound, timer);//参数为sound和timer,实现按下暂停，音乐停止，计时器停止，从而实现画面静止
    button->setPos(970, 20);
    scene->addItem(button);
    //设置场景
    Map *map = new Map;
    map->setPos(618, 326);
    scene->addItem(map);
    //用循环语句，设置5个一二车
    for (int i = 0; i < 5; ++i)
    {
        Caronetwo *caronetwo = new Caronetwo;
        caronetwo->setPos(210, 130 + 98 * i);
        scene->addItem(caronetwo);
    }
    //QGraphicsView提供的View组件，用于可视化场景中的内容
    //创建
    view = new QGraphicsView(scene, this);
    //设置大小
    view->resize(902, 602);
    //反走样功能，调用反锯齿功能，优化图像
    view->setRenderHint(QPainter::Antialiasing);
    //设置背景图
    view->setBackgroundBrush(QPixmap(":/picture/Background.jpg"));
    //进行画质优化
    view->setCacheMode(QGraphicsView::CacheBackground);
    //设置自动刷新模式
    view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    connect(timer, &QTimer::timeout, scene, &QGraphicsScene::advance);
    connect(timer, &QTimer::timeout, this, &Scene::addBubu);
    connect(timer, &QTimer::timeout, this, &Scene::check);
    //启动定时器
    timer->start(33);
    view->show();
}

Scene::~Scene()
{
    delete sound;
    delete timer;
    delete scene;
    delete view;
}

//绘制布布
void Scene::addBubu()
{
    static int low = 4;
    static int high = 8;
    static int maxtime = 20 * 1000 / 33;
    static int time = maxtime / 2;
    static int counter = 0;
    if (++counter >= time)
    {
        if (++low > high)
        {
            maxtime /= 1.3;
            high *= 2;
        }
        counter = 0;
        //生成随机数并进行运算
        time = rand() % (2 * maxtime / 3) + maxtime / 3;
        int type = rand() % 100;
        int i = rand() % 5;
        Bubu *Bubu;
        //根据随机数各个分布区间出现的概率不同，来随机绘制布布
        if (type < 40)
            Bubu = new Base_bubu;
        else if (type < 70)
            Bubu = new Basketbubu;
        else if (type < 80)
            Bubu = new Cockhorsebubu;
        else if (type < 90)
            Bubu = new Carbubu;
        else
            Bubu = new Bikebubu;
        Bubu->setPos(1028, 130 + 98 * i);
        scene->addItem(Bubu);
    }
}

//通过判断布布距离左边界的距离来判断是否游戏结束
void Scene::check()
{
    static int time = 1 * 1000 / 33;
    static int counter = 0;
    if (++counter >= time)
    {
        counter = 0;
        const QList<QGraphicsItem *> items = scene->items();
        foreach (QGraphicsItem *item, items)
            if (item->type() == Bubu::Type && item->x() < 200)
            {
                scene->addPixmap(QPixmap(":/picture/BubuWon.png"))->setPos(336, 50);
                scene->advance();
                timer->stop();
                return;
            }
    }
}


